package com.zhangyanqiang.rabbitmqdemo.exchange;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangyanqiang
 * @date 2020/4/19
 **/
@Configuration
public class DirectRabbitConfig {


    @Bean
    RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }


    /**
     * 生成队列
     * @return
     */
    @Bean
    public Queue DirectQueue(){
        Map<String,Object> map = new HashMap<>();
        return new Queue("directQueue",true,false,false);
    }

    @Bean
    public Queue ttlQueue(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-message-ttl",60000);
        return new Queue("ttlQueue",true,false,false,map);
    }

    @Bean
    public Queue ttlForQueue(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-expires",60000);
        return new Queue("ttlForQueue",true,false,false,map);
    }

    /**
     * queue 持久化
     * @return
     */
    @Bean
    public Queue DurableTrueQueue(){
        return new Queue("durableQueue",true);
    }

    /**
     * queue 非持久化
     * @return
     */
    @Bean
    public Queue DurableFalseQueue(){
        return new Queue("noDurableQueue",false);
    }

    @Bean
    public Queue exclusiveTrueQueue(){
        return new Queue("exclusiveTrueQueue",true,true,false);
    }

    @Bean
    public Queue exclusiveFalseQueue(){
        return new Queue("exclusiveFalseQueue",false,false,false);
    }

    @Bean
    DirectExchange testExchange(){
        return new DirectExchange("testExchange");

    }

    @Bean
    Binding bindingDirect(){
        return BindingBuilder.bind(DirectQueue()).to(testExchange()).with("testDirectRoutKey");
    }

    /**
     * 排他队列 绑定
     * @return
     */
    @Bean
    Binding bindingExclusiveTrue(){
        return BindingBuilder.bind(exclusiveTrueQueue()).to(testExchange()).with("exclusiveTrueQueueRoutKey");
    }

    @Bean
    Binding bindingExclusiveFalse(){
        return BindingBuilder.bind(exclusiveFalseQueue()).to(testExchange()).with("exclusiveFalseQueueKey");
    }
    @Bean
    Binding bindingQuorum(){
        return BindingBuilder.bind(quorumQueue()).to(testExchange()).with("quorumQueueKey");
    }

    @Bean
    Binding bindingTTlQueue(){
        return BindingBuilder.bind(ttlQueue()).to(testExchange()).with("ttlQueueKey");
    }

    @Bean
    Binding bindingTTLForQueue(){
        return BindingBuilder.bind(ttlForQueue()).to(testExchange()).with("ttlForQueueKey");
    }
    @Bean
    Queue quorumQueue(){

        Map<String,Object> map = new HashMap<>();
        map.put("x-queue-type","quorum");
        return new Queue("quorumQueue",true,false,false,map);
    }

    @Bean
    public Queue receiveTTLQueue(){
        Map<String,Object> map = new HashMap<>();
        return new Queue("receiveTTLQueue",true,false,false);
    }

    @Bean
    Binding bindingReceiveTTLQueue(){

        return BindingBuilder.bind(receiveTTLQueue()).to(testExchange()).with("receiveTTLQueueRoutKey");
    }

}
